package com.qny.ai.controller;

import com.qny.ai.common.ApiResponse;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/health")
public class HealthController {
    
    private final ChatClient chatClient;
    
    public HealthController(ChatClient chatClient) {
        this.chatClient = chatClient;
    }
    
    @GetMapping
    public ApiResponse<Map<String, Object>> health() {
        Map<String, Object> status = new HashMap<>();
        status.put("timestamp", LocalDateTime.now());
        status.put("status", "UP");
        
        // 测试DashScope连接
        try {
            String response = chatClient.prompt()
                    .messages(new UserMessage("测试"))
                    .call()
                    .content();
            status.put("dashscope", "OK");
            status.put("dashscope_response", response != null ? response.substring(0, Math.min(50, response.length())) : "null");
        } catch (Exception e) {
            status.put("dashscope", "ERROR");
            status.put("dashscope_error", e.getMessage());
        }
        
        return ApiResponse.ok(status);
    }
}
